package com.financing.platform.activities.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.platform.activities.ActivitiesBusinessObject;
import com.financing.platform.dao.IActivitesDao;
import com.financing.platform.dao.IActivitieGiftDao;
import com.financing.platform.dto.ActivitesDTO;
import com.financing.platform.dto.ActivitieGiftDTO;
import com.financing.platform.dto.FaUserDTO;

@Service("activitiesYLYXHDImpl")
public class ActivitiesYLYXHDImpl extends ActivitiesBusinessObject{

	protected Logger log =Logger.getLogger(this.getClass());
	//活动来源 2016 元宵活动  Code   
	//20160429 元宵活动第二波
	public final static String activitiesCode ="cd500";
	
	//活动来源 2016 元宵活动  默认分享人最多可领红包数
	public final static int activitiesCount =15;
	
	@Autowired
	private IActivitieGiftDao activitieGiftDaoImpl;
	
	@Autowired
	private IActivitesDao activitesDao;
	
	
	@Override
	public void handleProductFound(Object object) throws Exception {
	}

	@Override
	public void handleFaUserRegister(Object object)  throws Exception{
		
	}

	@Override
	@SuppressWarnings("unchecked")
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void handleFaUserAuth(Object object) throws Exception {
		 
		//因为红包是有额度的，多线程并发时需要同步处理，避免超红包额度，避免超红包重复领取
		synchronized (ActivitiesYLYXHDImpl.class) {
		
		Map<String,Object> param = (Map<String,Object>) object;
		FaUserDTO  faUserDTO = (FaUserDTO) param.get("faUserDTO");
		
		//查询活动信息
		ActivitesDTO activitesDTO = activitesDao.searchActivitesByCode(activitiesCode);
		if(activitesDTO == null){
			log.info("ActivitiesYLYXHDImpl handleFaUserAuth activitesDTO is null !!!");
			return;
		}
		
		//审核通过
		if(!"1".equals(faUserDTO.getuRole()) &&  !"99".equals(faUserDTO.getuRole()) && !"98".equals(faUserDTO.getuRole())){
			//查询自己的01:生效中红包
			ActivitieGiftDTO gift = new ActivitieGiftDTO();
			gift.setFaUserId(faUserDTO.getId());
			gift.setActiviesCode(activitiesCode);
			gift.setaSource("01");
			gift.setaStatus("01");
			List<ActivitieGiftDTO> list = activitieGiftDaoImpl.queryActivitiesGiftByCode(gift);
			for(ActivitieGiftDTO tmp : list){
				log.info("ActivitiesYLYXHDImpl handleFaUserAuth updateActivitieGift 02 !!!"+tmp.getFaUserId());
				//打开自己01:生效中红包-变成02：已生效
				tmp.setaStatus("02");
				tmp.setUpdateBy(faUserDTO.getUpdateBy());
				activitieGiftDaoImpl.updateActivitieGift(tmp);
				
				//更新活动已发放总金额
				ActivitesDTO dto = new ActivitesDTO();
				dto.setActiviesNowAmount(tmp.getaAmount());
				dto.setActiviesCode(activitiesCode);
				activitesDao.updateAcitivitesNowAmount(dto);
			}
				

			//查询分享人的01:生效中红包
			//判断分享人是否超出人数限制
			//打开分享人的01:生效中红包-变成02：已生效
			//更新活动已发放总金额
			gift.setFaShareUserId(faUserDTO.getId());
			gift.setFaUserId(null);
			gift.setActiviesCode(activitiesCode);
			gift.setaSource("02");
			gift.setaStatus("01");
			List<ActivitieGiftDTO> shareList = activitieGiftDaoImpl.queryActivitiesGiftByCode(gift);
			for(ActivitieGiftDTO tmp : shareList){
				
				//判断分享人是否超出人数限制
				gift.setFaUserId(tmp.getFaUserId());
				gift.setFaShareUserId(null);
				gift.setaSource("02");
				List<ActivitieGiftDTO> listShareUserCount = activitieGiftDaoImpl.queryActivitiesGiftByCode(gift);
				
				int listShareUserCountSize = 0;
				for (ActivitieGiftDTO bean:listShareUserCount) {
					//02：已生效，，04：提现  才算
					if("02".equals(bean.getaStatus())
							|| "04".equals(bean.getaStatus())){
						listShareUserCountSize ++;
					}
				}
				
				if(listShareUserCount == null || listShareUserCountSize < (activitesDTO.getActiviesCount()==null?activitiesCount:activitesDTO.getActiviesCount())){
					log.info("ActivitiesYLYXHDImpl handleFaUserAuth addActivitesGif aSource 02&&04 <=15!!!"+tmp.getFaUserId());
					
					//打开分享人的01:生效中红包-变成02：已生效
					tmp.setaStatus("02");
					tmp.setUpdateBy(faUserDTO.getUpdateBy());
					activitieGiftDaoImpl.updateActivitieGift(tmp);
					//更新活动已发放总金额
					ActivitesDTO dto = new ActivitesDTO();
					dto.setActiviesNowAmount(tmp.getaAmount());
					dto.setActiviesCode(activitiesCode);
					activitesDao.updateAcitivitesNowAmount(dto);
				}
			}
		}
		//审核失败
		if("99".equals(faUserDTO.getuRole())){
			//查询自己的冻结红包
			ActivitieGiftDTO gift = new ActivitieGiftDTO();
			//失效分享人的冻结红包
			gift.setFaShareUserId(faUserDTO.getId());
			gift.setActiviesCode(activitiesCode);
			gift.setaSource("02");
			gift.setaStatus("01");
			List<ActivitieGiftDTO> shareList = activitieGiftDaoImpl.queryActivitiesGiftByCode(gift);
			for(ActivitieGiftDTO tmp : shareList){
				log.info("ActivitiesYLYXHDImpl handleFaUserAuth updateActivitieGift 03!!!"+tmp.getFaUserId());
				//打开自己的冻结红包-变成失效  01-03
				tmp.setaStatus("03");
				tmp.setUpdateBy(faUserDTO.getUpdateBy());
				activitieGiftDaoImpl.updateActivitieGift(tmp);
			}
		}
	  }
	}

	@Override
	public void handleOrderRebate(Object object) throws Exception {
		
	}
	
}
